// 坐标系转换
import {lngLatToMercator, mercatorToLngLat} from './coordinateTransforms/bd09'
import {BD09toWGS84} from './coordinateTransforms/coordinate-transformation'

// 第三方坐标转换
import proj4 from 'proj4'
import {
  addProjection,
  addCoordinateTransforms,
  Projection,
  transform,
  fromLonLat,
  toLonLat
} from 'ol/proj'

/**
 * 自定义坐标系CGCS2000
 */
export function defsCGCS2000() {
  /**
   * 自定义坐标系4540
   * https://www.cnblogs.com/mhere/p/8574815.html
   * http://epsg.io/4540  88.15425,47.828473
   */
  proj4.defs(
    'EPSG:4326',
    '+proj=tmerc +lat_0=0 +lon_0=93 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
  )
  // 定义Openlayers坐标
  var projection_ = new Projection({
    code: 'EPSG:4543',
    //  extent : [-20026376.39, -15496570.74, 20026376.39, 18764656.23],
    units: 'm',
    axisOrientation: 'neu'
  })
  // 将坐标系4543添加到Openlayers坐标系
  addProjection(projection_)
}

/**
 * 注册百度坐标系
 */
export function defsBD09() {
  //定义一个坐标系
  var projBD09 = new Projection({
    code: 'BD:09',
    extent: [-20037726.37, -11708041.66, 20037726.37, 12474104.17],
    units: 'm',
    axisOrientation: 'neu',
    global: true
  })

  addProjection(projBD09)
  addCoordinateTransforms(
    'BD:09',
    'EPSG:3857',
    function (coordinate) {
      return lngLatToMercator(coordinate)
    },
    function (coordinate) {
      return mercatorToLngLat(coordinate)
    }
  )
}

/**
 * 注册WGS84标系
 */
export function defsWGS84() {
  //定义一个坐标系
  var projBD09 = new Projection({
    code: 'EPSG:4326',
    // extent: [-20037726.37, -11708041.66, 20037726.37, 12474104.17],
    units: 'm',
    axisOrientation: 'neu',
    global: true
  })

  addProjection(projBD09)
  addCoordinateTransforms(
    'BD:09',
    'EPSG:4326',
    function (coordinate) {
      return BD09toWGS84(coordinate)
    },
    function (coordinate) {
      return coordinate
    }
  )
}
